package com.zq.it.action;

import java.io.File;
import java.io.IOException;
import java.sql.Date;

import javax.annotation.Resource;

import com.zq.it.dao.common.PageConfig;
import com.zq.it.pojo.FileSource;
import com.zq.it.pojo.User;
import com.zq.it.pojo.exam.data.ObjectData;
import com.zq.it.service.FileSourceService;
import com.zq.it.tool.Constant;

import net.sf.json.JSONArray;

public class FileSourceAction extends BaseAction {

	private static final long serialVersionUID = 6931694597240284712L;
	@Resource
	private FileSourceService fileSourceService;
	private ObjectData objList;
	private FileSource fileSource;
	private long [] ids;
	private Integer amount;
	
	public void uploadSource() throws IOException {
		if (fileSource != null && this.getUpload() != null) {
			String fileName = getUploadFileName();
			String fileType = fileName.substring(fileName.lastIndexOf("."));
			fileName = System.currentTimeMillis() + fileType;
			if (upLoad(this.getUpload(), Constant.ARTICALE_PATH + File.separator + fileName)) {
				User user = (User)getHttpSession().getAttribute(Constant.USER);
				fileSource.setUploadUser(user.getAccount());
				fileSource.setUploadTime(new Date(System.currentTimeMillis()));
				fileSource.setUrl(fileName);
				fileSourceService.addFileSource(fileSource);
				print_html_utf8("<script>parent.location.reload();</script>");
			}
		} else {
			this.addFieldError("error", "上传文件不能为空！");
		}
	}
	
	public String read() {
		getRequest().setAttribute("url", fileSource.getUrl());
		return "online_read"; 
	}
	
	public String getMoreSources() {
		return "more_sources"; 
	}

	public void searchSources() {
		getRequest().setAttribute(Constant.ARTICLES_LIST, fileSourceService.getAllFileSource());
		User user  = (User) getHttpSession().getAttribute(Constant.USER);
		PageConfig config = new PageConfig();
		config.setHql("from FileSource as t where t.uploadUser = '"+ user.getAccount() + "'");
		config.setCountHql("select count(*) from FileSource as t where t.uploadUser = '"+ user.getAccount() + "'");
		config.setPage(page);
		config.setPageSize(Constant.PAGE_SIZE);
		config.setPerPageCount(Constant.PER_PAGE_COUNT);
		config.setUrl("paper_getPaperList.asp");
		objList = fileSourceService.getListByPage(config);
	}
	
	public void getAllSources() throws IOException {
		print_json_utf8(JSONArray.fromObject(fileSourceService.getAllFileSource(),Constant.getJcf("YYYY-MM-dd HH:mm:ss")).toString());
	}
	
	public String getStudentSources() {
		searchSources();
		return "student_sources";
	}
	
	public String getTeacherSources() {
		searchSources();
		return "teacher_sources";
	}
	
	public void deleteFileSources() throws IOException {
		fileSourceService.deleteFileSourceByIds(ids);
		this.print_text_utf8("ok");
	}
	
	public void getFixNumberSources() throws IOException {
		print_json_utf8(JSONArray.fromObject(fileSourceService.findSourcesByNumber(amount),Constant.getJcf("YYYY-MM-dd HH:mm:ss")).toString());
	}
	
	public String addSourcePage() {
		return "addSource";
	}
	
	public FileSource getFileSource() {
		return fileSource;
	}

	
	public Integer getAmount() {
		return amount;
	}

	public void setAmount(Integer amount) {
		this.amount = amount;
	}

	public void setFileSource(FileSource fileSource) {
		this.fileSource = fileSource;
	}

	public long[] getIds() {
		return ids;
	}

	public void setIds(long[] ids) {
		this.ids = ids;
	}

	public ObjectData getObjList() {
		return objList;
	}

	public void setObjList(ObjectData objList) {
		this.objList = objList;
	}
}
